<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Main extends CI_Controller {
 
    function __construct()
    {
        parent::__construct();
 
        $this->load->library('ion_auth');
        $this->load->helper('url');
        if (!$this->ion_auth->logged_in())
        {
            //redirect them to the login page
            redirect('account/login', 'refresh');
        }
        elseif (!$this->ion_auth->is_admin())
        {
            //redirect them to the home page because they must be an administrator to view this
            redirect($this->config->item('base_url'), 'refresh');
        }

        /* Standard Libraries of codeigniter are required */
        $this->load->database();
        
        /* ------------------ */ 

        $this->load->library('grocery_CRUD');
 
    }
 
    public function index()
    {
        $this->_example_output();
    }
 
    public function stages()
    {
        $prefix = $this->db->dbprefix;
        $this->grocery_crud->set_table($prefix.'stages');
        $output = $this->grocery_crud->render();
 
        $this->_example_output($output);
    }

    public function riders()
    {
      $prefix = $this->db->dbprefix;
        $this->grocery_crud->set_table($prefix.'riders');
        $this->grocery_crud->set_relation('team_number', $prefix.'teams', 'team_name');
        $this->grocery_crud->set_relation('type_id', $prefix.'rider_types', 'type_description');
        $this->grocery_crud->display_as('team_number', 'Team');
        $this->grocery_crud->display_as('type_id', 'Type');
        $output = $this->grocery_crud->render();
 
        $this->_example_output($output);
    }

    public function rider_types()
    {
        $prefix = $this->db->dbprefix;
        $this->grocery_crud->set_table($prefix.'rider_types');
        $output = $this->grocery_crud->render();
 
        $this->_example_output($output);
    }

    public function teams()
    {
        $prefix = $this->db->dbprefix;
        $this->grocery_crud->set_table($prefix.'teams');
        $output = $this->grocery_crud->render();
 
        $this->_example_output($output);
    }    

    public function points()
    {
        $prefix = $this->db->dbprefix;
        $this->grocery_crud->set_table($prefix.'points');
        $this->grocery_crud->set_relation('points_user_id', $prefix.'users', 'username');
        $this->grocery_crud->display_as('points_user_id', 'User');
        $output = $this->grocery_crud->render();
 
        $this->_example_output($output);
    }

    public function selections()
    {
        $prefix = $this->db->dbprefix;
        $this->grocery_crud->set_table($prefix.'selections');
        $this->grocery_crud->set_relation('user_id', $prefix.'users', 'username');
        $this->grocery_crud->display_as('user_id', 'User');
        $output = $this->grocery_crud->render();
 
        $this->_example_output($output);
    }    

    public function users()
    {
        $prefix = $this->db->dbprefix;
        $this->grocery_crud->set_table($prefix.'users');
        $output = $this->grocery_crud->render();
 
        $this->_example_output($output);
    }

    public function groups()
    {
        $prefix = $this->db->dbprefix;
        $this->grocery_crud->set_table($prefix.'groups');
        $output = $this->grocery_crud->render();
 
        $this->_example_output($output);
    }      

    public function config()
    {
        $prefix = $this->db->dbprefix;
        $this->grocery_crud->set_table($prefix.'config');
        $output = $this->grocery_crud->render();
 
        $this->_example_output($output);
    } 

    public function users_groups()
    {
        $prefix = $this->db->dbprefix;
        $this->grocery_crud->set_table($prefix.'users_groups');
        $this->grocery_crud->set_relation('user_id', $prefix.'users', 'username');
        $this->grocery_crud->set_relation('group_id', $prefix.'groups', 'name');
        $output = $this->grocery_crud->render();
 
        $this->_example_output($output);
    }  

    function _example_output($output = null)
 
    {
        $this->load->view('admindb/admin_db_template.php',$output);    
    }
}
 
/* End of file main.php */
/* Location: ./application/controllers/main.php */